package com.wenan.ui.controls.listBoxes
{
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.CheckBox;
	import mx.controls.DataGrid;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.controls.listClasses.ListData;
	import mx.events.DynamicEvent;
	import mx.utils.object_proxy;
	

	public class CheckBoxHeaderRender extends CheckBox
	{
		private var _data:DataGridColumn;
		private var isRegisted:Boolean = false;
		public var checkField:String = "selected";
		
		public function CheckBoxHeaderRender()
		{
			super();
			this.setStyle("paddingLeft", 5);
			this.addEventListener(MouseEvent.CLICK, this.onHeaderClick);
		}
		
		override public function set data(value:Object):void {
			_data = value as DataGridColumn;
			this.label = value.headerText;
			if (!this.isRegisted) {
				var cc:CheckBoxColumn = this.data as CheckBoxColumn;
				cc.header = this;
				this.checkField = cc.checkField;
				this.isRegisted = true;
			}
		}
		
		override public function get data():Object {
			return _data;
		}
		
		override public function set selected(value:Boolean):void {
			super.selected = value;
			if (listData) {
				var dg:DataGrid = DataGrid(listData.owner);
				var de:DynamicEvent = new DynamicEvent("selectedAllChange");
				de.selected = this.selected;
				dg.dispatchEvent(de);
			}
		}
		
		public function onHeaderClick(e:MouseEvent):void{
			var dg:DataGrid = DataGrid(listData.owner);
			var s:ArrayCollection = ArrayCollection(dg.dataProvider);
			var array:Array = [];
			for (var i:int=0; i<s.length; i++) {
				s[i][this.checkField] = this.selected;
			}
			dg.invalidateList();
		}
	}
}